<?php
 namespace app\mobile\controller;

 use app\common\logic\CartLogic;
 use app\common\logic\MessageLogic;
 use app\common\logic\UsersLogic;
 use app\common\logic\OrderLogic;
 use app\common\model\Order as OrderModel;
 use app\common\util\TpshopException;
 use think\Page;
 use think\Verify;
 use think\Loader;
 use think\db;
class Report extends MobileBase{

    public $user_id = 0;
    public $user = array();

    /*
    * 初始化操作
    */
    public function _initialize()
    {
        parent::_initialize();
        if (session('?user')) {
            $session_user = session('user');
            $select_user = M('users')->where("user_id", $session_user['user_id'])->find();
            $oauth_users = M('OauthUsers')->where(['user_id'=>$session_user['user_id']])->find();
            empty($oauth_users) && $oauth_users = [];
            $user =  array_merge($select_user,$oauth_users);
            session('user', $user);  //覆盖session 中的 user
            $this->user = $user;
            $this->user_id = $user['user_id'];
            $this->assign('user', $user); //存储用户信息
        }
        $nologin = array(
            'login', 'pop_login', 'do_login', 'logout', 'verify', 'set_pwd', 'finished',
            'verifyHandle', 'reg', 'send_sms_reg_code', 'find_pwd', 'check_validate_code',
            'forget_pwd', 'check_captcha', 'check_username', 'send_validate_code', 'express' , 'bind_guide', 'bind_account','bind_reg'
        );
        $is_bind_account = tpCache('basic.is_bind_account');
        if (!$this->user_id && !in_array(ACTION_NAME, $nologin)) {
            if(strstr($_SERVER['HTTP_USER_AGENT'],'MicroMessenger') && $is_bind_account){
                header("location:" . U('Mobile/User/bind_guide'));//微信浏览器, 调到绑定账号引导页面
            }else{
                header("location:" . U('Mobile/User/login'));
            }
            exit;
        }

        $order_status_coment = array(
            'WAITPAY' => '待付款 ', //订单查询状态 待支付
            'WAITSEND' => '待发货', //订单查询状态 待发货
            'WAITRECEIVE' => '待收货', //订单查询状态 待收货
            'WAITCCOMMENT' => '待评价', //订单查询状态 待评价
        );
        $this->assign('order_status_coment', $order_status_coment);
    }

    /*
     * 用户中心首页
     */
    public function index()
    {
        $user_id =$this->user_id;
        $logic = new UsersLogic();
        $user = $logic->get_info($user_id); //当前登录用户信息
        $comment_count = M('comment')->where("user_id", $user_id)->count();   // 我的评论数
        $level_name = M('user_level')->where("level_id", $this->user['level'])->getField('level_name'); // 等级名称
        //获取用户信息的数量
        $messageLogic = new MessageLogic();
        $user_message_count = $messageLogic->getUserMessageCount();
        $user['result']['order_money'] = $user['result']['next_ordermoney']  + $user['result']['second_ordermoney'];
        $this->assign('user_message_count', $user_message_count);
        $this->assign('level_name', $level_name);
        $this->assign('comment_count', $comment_count);
        $this->assign('user',$user['result']);
        return $this->fetch();
    }
    public function achieve()
    {
        $order_statusArr = C('ORDER_STATUS');
        $user_id =$this->user_id;
        $logic = new UsersLogic();
        $user = $logic->get_info($user_id); //当前登录用户信息
        //获取用户信息的数量
        $parentid = $user_id;
        $condition['parent_id'] = $parentid;
        $condition['distribut_level'] = 3;
        $adminres = Db::name('users')->find($parentid);
        $useridArr1 =  Db::name('users')->where($condition)->column('user_id');
        $mzIdArr  = '';
        $order_where = ' 1 = 1 and ';
        if($useridArr1){
            $mzIdArr =implode($useridArr1);
        }else{
            $order_where = ' 1 = 0 ';
        }

        if($adminres['distribut_level']==1){
            $fields = Db::name('users')->where('parent_id',$parentid)->column('user_id');
            $userIds = implode($fields);
            $conditionz['user_id'] = array('in',$userIds);
            $useridArr2 =  Db::name('users')->where($conditionz)->column('user_id');
            if($useridArr2){
                $mzIdArr =  $mzIdArr . ','.implode($useridArr2);
            }
        }
        if($mzIdArr){
            $order_where .= " o.user_id in ($mzIdArr) ";
        }

//        $begin =  strtotime("-1 month");//30天前
//        $end_time = $begin+24*60*60*30;
//        $order_where .= "o.add_time>$begin and o.add_time<$end_time";  //交易成功的有效订单
        $order_count = Db::name('order')->alias('o')->where($order_where)->whereIn('order_status','1,2,4')->count();
        $Page = new Page($order_count,20);
        $order_list = Db::name('order')->alias('o')
            ->field('o.fx_record,o.order_id,o.order_sn,o.goods_price,o.shipping_price,o.order_status,o.total_amount,o.add_time,u.user_id,u.nickname')
            ->join('users u','u.user_id = o.user_id','left')
            ->where($order_where)
            ->whereIn('order_status','1,2,4')
            ->limit($Page->firstRow,$Page->listRows)->select();
        foreach ($order_list as $key =>&$item){
            if($item['fx_record']){
                $item['fx_record'] = unserialize( $item['fx_record'] );
                $item['per'] = $item['fx_reacord'][$user_id]['per'];
                $item['permoney'] = $item['fx_reacord'][$user_id]['money'];
            }else{
                $item['per'] = 6;
                $item['permoney'] = round($item['total_amount']*6/100,2);
            }
            $item['order_status'] = $order_statusArr[$item['order_status']];

        }
        $this->assign('order_list',$order_list);
        $this->assign('order_count',$order_count);
        $this->assign('page',$Page);
        $user['result']['order_money'] = $user['result']['next_ordermoney']  + $user['result']['second_ordermoney'];
        $this->assign('user',$user['result']);
        return $this->fetch();
    }
    public function help(){
        return $this->fetch();

    }
    public function order_detail()
    {
        $id = input('id/d', 0);
        $Order = new OrderModel();
        $order = $Order::get(['order_id' => $id]);
        $this->assign('order', $order);
        return $this->fetch();
    }

    public function details(){
        $article_id = I('GET.article_id',0);
        $article = Db::name('article')->where("article_id", $article_id)->find();
        $this->assign('article', $article);
        return $this->fetch();
    }
    public function tixian(){
        if($this->request->isGet()){
            $id = $this->request->param('id');
            $reamin_money = $this->request->param('reamin_money');

            if($id && $reamin_money){
                $this->assign('reamin_money', $reamin_money);
                $this->assign('id', $id);
                return $this->fetch();
            }else{
                $data = array('status' => 0, 'msg' => '暂无提现金额', 'data' => '');
                return json($data);
            }
        }else{
            $id = $this->request->param('id');
            $userres = Db::name('users')->find($id);
            $money = $this->request->param('money');
            $res = Db::name('user_txrecord')->where('uid',$id)->where('type',0)->select();
            if($res){
                $data = array('status' => 0, 'msg' => '无法提现,已有一笔提现申请在审核中', 'data' => '');
            }else{
                $record['uid'] = $id;
                $record['nickname'] = $userres['nickname'];
                $record['type'] = 0;
                $record['record'] = '申请提现'.$money.'元';
                $record['money'] = $money;
                $record['add_time'] = time();
                $res = Db::name('user_txrecord')->insertGetId($record);
                if($res){
                    $data = array('status' => 1, 'msg' => '提现申请成功', 'data' => '');
                }else{
                    $data = array('status' => 0, 'msg' => '提现申请失败', 'data' => '');
                }

            }
            return json($data);
        }
    }
}
?>